package phoenix.web.test_objects;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.core.data_models.UserProfileDataModel.AboutMe;
import phoenix.core.data_models.UserProfileDataModel.LookingFor;
import phoenix.web.test_objects.data_providers.FunnelDataProviders;

/**
 * Class contains test methods for FunnelPage
 */
public class FunnelTests extends TestBaseWebExt
{

	public FunnelTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public FunnelDataProviders dataProviders = new FunnelDataProviders(manager);
	private final SearchTests searchTests = new SearchTests(manager);
	private final BackendTests backendTests = new BackendTests(manager);

	boolean p_expected = true;

	/**
	 * Confirmation test
	 */
	public void confirmationTest()
	{
		try
		{
			wd().openPage("/site/autologin?open=search&key="
				+ manager.testUser().getUserKey()
				+ "&emlTrack=1394582400-2a237ac5417111e39df290b11c05f0b9-TWF0Y2hSZXBlYXRhYmxl-ZUVtYWls");
			page().searchPage().waitForPageLoad();
			manager.testUser().setConfirmDate(manager.cronos().getCurrentDate("yyyy-MM-dd HH:mm:ss"));
			db().updateUserConfirmDate(manager.testUser());
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("confirmationTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	public void funnelTest(String p_screenname, LookingFor p_looking, AboutMe p_about)
	{
		try
		{
			wd().openAutologin(testUser());

			if (!wd().getCurrentUrl().contains("funnel"))
			{
				wd().openPage("/funnel");
			}

			if (manager.isBN() || manager.isCNF() || manager.isFlirt() || manager.isLGW() || manager.isNF() || manager.isWB() || manager.isMD())
			{
				page().funnelPage().fillStepOne(p_screenname, p_looking);
			}

			if (manager.isFGF())
			{
				page().funnelPage().fillStepOneFGF(p_screenname, p_about);
			}

			if (manager.isCNF() || manager.isLGW() || manager.isNF() || manager.isMD())
			{
				page().funnelPage().setDistance(p_looking);
			}

			page().funnelPage().clickNextStep();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("funnelTest: " + p_ex.toString());
		}
		finally
		{
			db().updateUserData(testUser());
			test().forErrors();
		}

	}

	public void photoUploadMotivationTest()
	{
		if (manager.testModel().getBrowserName().equals(manager.config().BROWSER_IEXPLORER))
		{
			log().warn("photoUploadMotivationTest temporarily disabled for BROWSER_IEXPLORER");
			return;
		}

		try
		{
			page().funnelPage().uploadPhoto();

			page().funnelPage().waitForPhoto();
			if (!page().funnelPage().isPhotoPresent())
			{
				log().errorAndScreen("Photo isn't uploaded");
			}

			if (!manager.isFGF())
			{
				page().funnelPage().removePhoto();

				if (page().funnelPage().isPhotoPresent())
				{
					log().errorAndScreen("Photo image isn't removed");
				}

				page().funnelPage().uploadPhoto();
				page().funnelPage().waitForPhoto();
				if (!page().funnelPage().isPhotoPresent())
				{
					log().errorAndScreen("Photo image isn't uploaded");
				}
			}

			page().funnelPage().clickSave();
			backendTests.approvePhoto(testUser());

			wd().openURL(testUser().getAutologin());
			wd().openPage("/profile");
			page().myProfilePage().waitForPageLoad();

			if (!page().myProfilePage().isAvatarPresent())
			{
				log().errorAndScreen("Top menu avatar isn't displayed after funnel");
			}

			if ((manager.isBN() || manager.isWB()))
			{
				page().myProfilePage().clickUploadPhotoMenu();
			}

			page().myProfilePage().waitForPhotoElement();

			if (!page().myProfilePage().isPhotoPresent())
			{
				log().errorAndScreen("My profile photo image isn't displayed after funnel");
			}

			if (!manager.isFGF())
			{
				page().myProfilePage().removePhoto();

				if (page().myProfilePage().isPhotoPresent())
				{
					log().errorAndScreen("My profile photo image isn't remuved");
				}
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("photoUploadMotivationTest: " + p_ex.toString());
		}

		test().forErrors();
	}
}